import 'package:center_beijing/common/styles.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/default_style.dart';
import 'package:flutter_pickers/time_picker/model/date_mode.dart';
import 'package:flutter_pickers/time_picker/model/suffix.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_pickers/style/default_style.dart';
import 'package:flutter_pickers/time_picker/model/date_mode.dart';
import 'package:flutter_pickers/time_picker/model/pduration.dart';
import 'package:flutter_pickers/time_picker/model/suffix.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:images_picker/images_picker.dart';
import 'dart:io';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class SelectRow extends StatefulWidget {
  final String title;
  final String icon;
  final Function selectCallback;
  final String rightText;
  final bool bottomLine;
  final bool require;
  final double myWidth;
  final double myHeight;
  final String currentTime;
  final DateMode selectType;
  final double titleFont;
  final Color titleColor;
  SelectRow(
      {@required this.title,
        this.myWidth=32,
        this.selectType = DateMode.YMDHM,
        this.titleFont,
        this.currentTime,
        this.titleColor = Styles.DetailColor,
        this.myHeight=32,
      this.icon = '',
      this.selectCallback,
      this.rightText='',
      this.bottomLine = false,this.require = false});

  @override
  _SelectRowState createState() => _SelectRowState();
}

class _SelectRowState extends State<SelectRow> {
  @override
  Widget build(BuildContext context) {
    DateTime temTime =((widget.rightText!=null&&widget.rightText!='')?DateTime.parse(widget.rightText):DateTime.now());
    return GestureDetector(
      child: Container(

        height: Styles.MainSelectRowHeight,
        child: DecoratedBox(
          decoration: BoxDecoration(
              border: widget.bottomLine
                  ? Border(
                  bottom: BorderSide(color: Styles.LineColor, width: 1))
                  : null),
          child: Padding(
            padding: EdgeInsets.fromLTRB(Styles.spacing8, 0, 16, 0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    widget.require
                        ? Text(
                      '*',
                      style: TextStyle(color: Colors.red),
                    )
                        : Container(),
                    widget.icon != ''
                        ? Container(
                      padding: EdgeInsets.fromLTRB(Styles.spacing4, 0, Styles.spacing4, 0),
                      child: Image(
                        image: AssetImage(widget.icon),
                        width: widget.myWidth,
                        height: widget.myHeight,
                        fit: BoxFit.contain,
                      ),
                    )
                        : Container(),
                    Padding(
                      padding: EdgeInsets.all(0),
                      child: Text(
                        widget.title,
                        style: TextStyle(
                            color: widget.titleColor,
                            fontSize: widget.titleFont==null?Styles.MiddleFont17:widget.titleFont),
                      ),
                    ),
                  ],
                ),
                Row(
                  children: <Widget>[
                    Text(
                      (widget.rightText == null||widget.rightText == '') ? '请选择' : widget.rightText,
                      style: TextStyle(
                          fontSize: widget.titleFont==null?Styles.MiddleFont17:widget.titleFont,
                          color: (widget.rightText == null||widget.rightText == '')?widget.titleColor:Styles.DetailColor),
                    ),
                    Image(
                      image: AssetImage('assets/images/right_arrow.png'),
                      width: 20.w,
                      height: 20.h,
                      fit: BoxFit.contain,
                      color: widget.titleColor,
                    )
                  ],
                )
              ],
            ),
          ),
        ),
      ),
      onTap: () {
        Pickers.showDatePicker(
          context,
          // 模式，详见下方
          mode:widget.selectType,
          // 后缀 默认Suffix.normal()，为空的话Suffix()
          suffix: Suffix(hours: ' 时', minutes: ' 分',),
          // minDate: PDuration(year: temTime.year,month: temTime.month,day: temTime.day,),
          // 样式  详见下方样式
          pickerStyle: DefaultPickerStyle(),
          selectDate: PDuration(year: temTime.year,month: temTime.month,day: temTime.day,hour: temTime.hour,minute: temTime.minute),
          // 默认选中
          onConfirm: (p) {
            FocusScope.of(context).requestFocus(FocusNode());
            var temTime = p.year.toString() +
                '-' +
                (p.month > 9
                    ? p.month.toString()
                    : ('0' + p.month.toString())) +
                '-' +
                (p.day > 9
                    ? p.day.toString()
                    : ('0' + p.day.toString()))
                + (widget.selectType!=DateMode.YMD?(' ' +
                    (p.hour > 9
                        ? p.hour.toString()
                        : ('0' + p.hour.toString())) +
                    ':' +
                    (p.minute > 9
                        ? p.minute.toString()
                        : ('0' + p.minute.toString())) +
                    ':' +
                    (p.second > 9
                        ? p.second.toString()
                        : ('0' + p.second.toString()))):'');
            widget.selectCallback(temTime);
            print('longer >>> 返回数据：$p');
          },
          // onChanged: (p) => print(p),
        );
      },
    );
  }
}
